package Application.Tool;

import Application.AppJoint;
import Application.Controller.RandomPenSettingsController;
import Application.Mask.MaskManager;
import Application.StatusBarManager.StatusBarManager;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import Application.LayerImage.LayerImage;
import javafx.scene.input.MouseEvent;
import Application.Layer.Layer;
import Application.Layer.LayerDelegate;

public class RandomPen extends Tool {
    private Parent root;
    private static RandomPen ins = new RandomPen();


    private LayerImage mask;
    private int size = 32;
    private boolean AEnabled = true;
    private boolean REnabled = true;
    private boolean GEnabled = true;
    private boolean BEnabled = true;
    private int AMin = 0, AMax = 255;
    private int RMin = 0, RMax = 255;
    private int GMin = 0, GMax = 255;
    private int BMin = 0, BMax = 255;

    int blend(int c1, int c2) {
        int ans = 0;
        int a1 = (c1 >> 24) & 0xff;
        int r1 = (c1 & 0x00ff0000) >> 16;
        int g1 = (c1 & 0x0000ff00) >> 8;
        int b1 = (c1 & 0x000000ff);
        int a2 = (c2 >> 24) & 0xff;
        int r2 = (c2 & 0x00ff0000) >> 16;
        int g2 = (c2 & 0x0000ff00) >> 8;
        int b2 = (c2 & 0x000000ff);

        int a = a1 + a2;
        if(a > 255) {
            a = 255;
        }

        int r = (a1 * r1 + (255 - a1) * r2) / 255;
        int g = (a1 * g1 + (255 - a1) * g2) / 255;
        int b = (a1 * b1 + (255 - a1) * b2) / 255;

        ans = (a << 24) | (r << 16) | (g << 8) | b;

        return ans;
    }

    public void setMask(String maskName) {
        this.mask = MaskManager.getInstance().getMaskByName(maskName);
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setA(boolean enabled, int m, int M) {
        this.AEnabled = enabled;
        this.AMax = M;
        this.AMin = m;
    }

    public void setR(boolean enabled, int m, int M) {
        this.REnabled = enabled;
        this.RMax = M;
        this.RMin = m;
    }

    public void setG(boolean enabled, int m, int M) {
        this.GEnabled = enabled;
        this.GMax = M;
        this.GMin = m;
    }

    public void setB(boolean enabled, int m, int M) {
        this.BEnabled = enabled;
        this.BMax = M;
        this.BMin = m;
    }

    public static RandomPen getInstance() {
        return ins;
    }

    private RandomPen() {
        super();

        // 加载本工具的工具设置
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/RandomPenSettings.fxml"));
            root = loader.load();

            AppJoint.getInstance().loadMasksToComboBox(
                    ((RandomPenSettingsController)loader.getController()).getCombo()
            );
        } catch(Exception e) {
            e.printStackTrace();
        }

        mask = MaskManager.getInstance().getMaskByName("test");
    }

    @Override public void onMouseDown(MouseEvent e) {
        onMouseMove(e);
    }

    @Override public void onMouseMove(MouseEvent e) {
        // 点击在imageview中的位置在实际图片中的像素位置
        double pixelx =
                e.getX() / AppJoint.getInstance().getImageView().getFitWidth() * AppJoint.getInstance().getCanvasWidth();
        double pixely =
                e.getY() / AppJoint.getInstance().getImageView().getFitHeight() * AppJoint.getInstance().getCanvasHeight();

        int activeid = AppJoint.getInstance().getActiveLayerId();
        if(activeid==-1){
            //System.out.println("你必须新建一个图层！");
            //Pen.getInstance().getMain().showWarnningWindow("你必须新建一个图层！");
            StatusBarManager.getInstance().setText("你必须新建一个图层！");

        }else {
            Layer activeLayer = LayerDelegate.getInstance().getLayerById(activeid);
            for (int i = -size / 2; i < size / 2; i++) {
                for (int j = -size / 2; j < size / 2; j++) {
                    int a = AEnabled ? (int) (AMin + Math.random() * (AMax - AMin + 1)) : AMin;
                    int r = REnabled ? (int) (RMin + Math.random() * (RMax - RMin + 1)) : RMin;
                    int g = GEnabled ? (int) (GMin + Math.random() * (GMax - GMin + 1)) : GMin;
                    int b = BEnabled ? (int) (BMin + Math.random() * (BMax - BMin + 1)) : BMin;
                    // 实际像素位置
                    int imgx = (int) pixelx + i;
                    int imgy = (int) pixely + j;
                    if (imgx >= 0 && imgx < AppJoint.getInstance().getCanvasWidth() && imgy >= 0 && imgy < AppJoint.getInstance().getCanvasHeight()) {
                        // 蒙版像素位置
                        int maskx = (i + size / 2) * MaskManager.MASK_SIZE / size;
                        int masky = (j + size / 2) * MaskManager.MASK_SIZE / size;
                        // 蒙版灰度
                        int gray = (mask.getPixelArgb(maskx, masky) & 255);
                        int alpha = gray * a / 255;

                        int oldColor = activeLayer.getArgb(imgx, imgy);
                        int srcColor = (alpha << 24) | (r << 16) | (g << 8) | b;

                        int finalColor = this.blend(srcColor, oldColor);

                        activeLayer.setArgb(imgx, imgy, finalColor);
                        LayerDelegate.getInstance().refreshSingle(imgx, imgy);
                    }
                }
            }
        }
    }

    @Override public void onMouseUp(MouseEvent e) {
        //flag = 0;
    }

    @Override public Parent getToolSettings() {
        return root;
    }
}
